﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Lilium;
using Lilium.Web.Mvc.EntityFilters;

namespace Lilium.Web.Mvc.Html
{
	public static class PagerExtensions
	{
		public static MvcHtmlString PagerActionLink(this HtmlHelper html, string linkText, PagerAction action)
		{
			if (html == null) throw new ArgumentNullException("html");

			var pager = GetPager(html);
			return EntityFilterExtensions.FilterActionLink(html, linkText, pager, Enum.GetName(typeof(PagerAction), action));
		}

		public static IPager GetPager(this HtmlHelper html)
		{
			if (html == null) throw new ArgumentNullException("html");

            var modelMetadata = html.ViewData.ModelMetadata;

            if (modelMetadata == null)
                throw new InvalidOperationException("Model metadata required.");

            var modelType = modelMetadata.ModelType;
			var entityType = modelType.GetGenericArguments()[0];

			return Pager.GetPager(html.ViewContext.Controller, entityType);
		}
	}
}
